The education front
By Grant Kwai
Copyright (c) 1992 Apple Users' Group, Sydney
Republished from Applecations, a publication of the Apple Users' Group, Sydney, Australia.
Welcome to the second of the series 'On the Education Front'. In this article, I am going to show you how to print all of the prime numbers from 1 to 500, although you can change this to another figure if you like. Firstly though, what is a prime number? Well, it is a number which is only exactly divisible by itself and one (e.g. 3, 5 and 17 are prime numbers).
You may well ask why would we want such a program? Well, there are two simple reasons. Firstly, a computer program can calculate prime numbers very quickly and two, a repetitive program, such as this, is a good test to see how fast your system is running (i.e. try it on a //e and then a //gs and see the difference!).
For those of you who might like to 'beat the computer', you might also like to slow down the speed of output to the screen down. Enter this command at the AppleSoft prompt:
SPEED= X
where 'X' is a number between 1 and 255. 255 is the fastest and normal value. What you can do is slow the computer down to a sufficient rate then try and write down all of the prime numbers between say 1 to 100 faster than the computer can print them while making sure you don't miss any. You can then check once you have finished that you haven't missed any.
Listing 1
---------
NEW
10 REM -=-=-=-=-=-=-=-=-=-=-=-=-=
20 REM = PRIME NUMBERS PROGRAM -
30 REM - BY GRANT KWAI =
40 REM = WRITTEN IN 1990 -
50 REM -=-=-=-=-=-=-=-=-=-=-=-=-=
60 REM
70 PRINT CHR$(4);"PR#3"
80 PRINT : PRINT" LET THE GAMES BEGIN!:"
90 FOR A = 1 TO 500
100 FOR B = 2 TO 250
110 LET C = A / B
120 LET D= INT(C)
130 IF D = 0 THEN 180
140 IF D = 1 THEN 170
150 IF C > M THEN 170
160 IF C = M THEN 190
170 NEXT B
180 PRINT " ";A;
190 NEXT A
200 PRINT "NO MORE PRIME NUMBERS IN RANGE"
210 END
Explanation
-----------
Line
10-60 REMinder statements
70 Turn on 80 column mode. (If no 80 column, then change line to 'HOME'
80 PRINTs message to screen
90 Sets range of search. (In this case, up to number 500). Variable 'A'
100 Sets number to divide number (the one set above) by. Variable 'B'
110 Sets 'P' to 'A' divided by 'B'.
120 Sets 'D' to an INTeger of 'C'.
130 Checks to see if 'D'=0. i.e. exactly divisible. If so, go to line 180
140 Checks to see if 'D'=1. i.e. divisible by itself. If so, go to line 170
150 Checks if 'C' is greater than 'D'. If so, go to line 170
160 Checks to see if 'C' is equal to 'D'. ie 'C' and 'D' are the same. If so go to line 190
170 Tells computer to move onto next value of 'B'
180 PRINTs a space, then the prime number to screen.
190 Tells computer to move onto next value of 'A' to try.
200 PRINTs to screen saying no more prime numbers in range.
210 ENDs program.
NOTE: You may like to modify the program to let the user select the range for the prime numbers. This is set in lines 90-100. Line 90 sets the range of the numbers and line 100 sets the range to divide by. (Notice that it starts at 2 and ends at half of the range set in line 90. Otherwise you would be repeating the divisions). Just make sure that if someone enters an odd number (e.g. 499), that you round it to the nearest even number.

THIS CONTENT COPYRIGHT © 2007, APPLE MACINTOSH USERS' GROUP, SYDNEY
Permission has been obtained to make this material available on the Internet.

Permission is hereby granted for non-profit user groups to republish this content.
PLEASE CREDIT THE AUTHOR AND THE SOURCE: Applecations, publication of the Apple Users' Group, Sydney, Australia

THIS PAGE COPYRIGHT © 2007, ANDREW ROUGHAN